/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package hci;

import backend.Subcategories;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import javax.swing.ActionMap;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import org.jdesktop.application.Action;
import org.jdesktop.application.Application;
import org.jdesktop.application.ApplicationContext;

/**
 *
 * @author Capx
 */
public class ListarSubCategorias {

    private JPanel contenedor;
    private JPanel anterior;
    private JPanel subcategorias;
    private JList lista;
    private int categoria;

    ListarSubCategorias(JPanel contenedor, JPanel anterior, int categoria){
        this.contenedor = contenedor;
        this.categoria = categoria;
        this.anterior = anterior;
    }

    @Action
    public void volver(ActionEvent e){
        contenedor.remove(subcategorias);
        anterior.setVisible(true);
    }

    @Action
    public void home(ActionEvent e){
        contenedor.remove(subcategorias);
        contenedor.getComponent(0).setVisible(true);
    }

    public void initialize(){
        Subcategories ca = new Subcategories(categoria);
        ca.initialize();

        String[] listaSubcategorias = ca.getSubcategories();

        ApplicationContext ctx = Application.getInstance().getContext();
        ActionMap map = ctx.getActionMap(this);

        subcategorias = new JPanel();
        subcategorias.setLayout(new GridLayout(0,1));
        subcategorias.add(new JLabel("Elija una subcategoria"));
        /*El GridLayout fija a todas las componentes del mismo tamaño
        **entonces como la lista es muy grande hace todos los botones
        **grandes!! MALDICION!!(BIS)*/
        lista = new JList(listaSubcategorias);
//        JScrollPane pane = new JScrollPane(lista);
//        subcategorias.add(pane);
        subcategorias.add(lista);

        subcategorias.add(new JLabel(""));

/*        JButton sub = new JButton();
        sub.setAction(map.get("subcategorias"));
        sub.setText("Subcategorias");*/
        JButton volver = new JButton();
        volver.setAction(map.get("volver"));
        volver.setText("Volver");
        JButton home = new JButton();
        home.setAction(map.get("home"));
        home.setText("Menu Principal");
//        categorias.add(sub);
        subcategorias.add(volver);
        subcategorias.add(home);

        contenedor.add(subcategorias);
    }
    
}
